<<<<<<< HEAD
package com.hsx.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class CookieDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //设置编码防止乱码控制台
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        Cookie[] cookies = req.getCookies();//得到cookies数组
        if(cookies!=null){//如果cookies不为空
            out.print("您上一次访问时间是");
            for (int i = 0; i <cookies.length ; i++) {//挨个输出cookies数组里的数据
                Cookie cookie = cookies[i];
                if(cookie.getName().equals("LastLoginTime")){//如果cookie对的名字和LastLoginTime相等
                    String value = cookie.getValue();
                    long l = Long.parseLong(value);
                    Date date = new Date(l);
                    out.print(date.toLocaleString());
                }
            }
        }else {
            out.print("欢迎您首次访问");
        }
        Cookie cookie = new Cookie("LastLoginTime",System.currentTimeMillis()+"");
        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
=======
package com.hsx.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class CookieDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //设置编码防止乱码控制台
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        Cookie[] cookies = req.getCookies();//得到cookies数组
        if(cookies!=null){//如果cookies不为空
            out.print("您上一次访问时间是");
            for (int i = 0; i <cookies.length ; i++) {//挨个输出cookies数组里的数据
                Cookie cookie = cookies[i];
                if(cookie.getName().equals("LastLoginTime")){//如果cookie对的名字和LastLoginTime相等
                    String value = cookie.getValue();
                    long l = Long.parseLong(value);
                    Date date = new Date(l);
                    out.print(date.toLocaleString());
                }
            }
        }else {
            out.print("欢迎您首次访问");
        }
        Cookie cookie = new Cookie("LastLoginTime",System.currentTimeMillis()+"");
        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
>>>>>>> 0955e43271ec003176a75da3f24c6d9c0071fe9b
